package mest.controller.cxj;


import com.alibaba.fastjson.JSON;
import mest.VO.cxj.FinancialCostOrderRecordVO;
import mest.entity.FinancialCostOrder;
import mest.entity.FinancialCostOrderRecord;
import mest.entity.PlanCostOrder;
import mest.service.Modeling.AccountFileService;
import mest.service.cxj.DictionariesOrderService;
import mest.service.cxj.FinancialCostOrderRecordService;
import mest.service.cxj.FinancialCostOrderService;
import mest.service.cxj.PlanCostOrderService;
import mest.service.system.DBService;
import mest.util.MoneyUtil;
import mest.util.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("financialCostOrderRecord")
@CrossOrigin
public class FinancialCostOrderRecordController {

    @Autowired
    private FinancialCostOrderRecordService financialCostOrderRecordService;

    @Autowired
    private DBService dbService;

    /**
     * 查询全部财务费用单记录
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("selectAll")
    public String selectAll(@RequestParam Integer page, @RequestParam Integer limit, @RequestParam Integer fcId) {
        ResultObj resultObj = null;
        String theirDb = dbService.getCompanyDB();
        try {
            List<FinancialCostOrderRecordVO> financialCostOrderRecordVOList = financialCostOrderRecordService.selectAllFinancialCostOrderRecordById(page, limit, fcId, theirDb);
            Integer count = financialCostOrderRecordService.selectCountFinancialCostOrderRecordById(fcId, theirDb);
            resultObj = new ResultObj(0, "success", count, financialCostOrderRecordVOList);
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }

    /**
     * 新增财务费用单记录
     *
     * @param fcor
     * @return
     */
    @PostMapping("insert")
    public String insert(@RequestBody FinancialCostOrderRecord fcor) {
        String theirDb = dbService.getCompanyDB();
        Integer userId = dbService.getUserId(); //获取操作人ID
        fcor.setEfId(userId);//设置当前操作人
        ResultObj resultObj = null;
        try {
            if (financialCostOrderRecordService.insertFinancialCostOrderRecord(fcor, theirDb) > 0) {
                resultObj = ResultObj.builder().code(0).msg("支付成功").build();
            } else {
                resultObj = ResultObj.builder().code(0).msg("支付失败").build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }

}
